<?php

namespace App\Admin\Controllers;

use App\Models\Movie;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MovieController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '电影';

    protected function grid()
    {
        $grid = new Grid(new Movie);

        // 创建一个列名为 ID 的列，内容是用户的 id 字段
        $grid->id('ID');

        // 创建一个列名为 用户名 的列，内容是用户的 name 字段。下面的 email() 和 created_at() 同理
        // $grid->title('电影标题');
        $grid->title(__('Title'));


        $grid->director(__('Director'));
        $grid->describe(__('Describe'));
        $grid->rate(__('Rate'));

        // $grid->released('是否上映')->display(function ($value) {
        //     return $value ? '是' : '否';
        // });
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];

        $grid->column('released', __('Released'))->switch($states);

        $grid->release_at(__('Release_at'));

        $grid->created_at(__('Created_at'));

        // 不在页面显示 `新建` 按钮，因为我们不需要在后台新建用户
        // $grid->disableCreateButton();
        // // 同时在每一行也不显示 `编辑` 按钮
        // $grid->disableActions();

        // $grid->tools(function ($tools) {
        //     // 禁用批量删除按钮
        //     $tools->batch(function ($batch) {
        //         $batch->disableDelete();
        //     });
        // });
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            //$filter->disableIdFilter();
        
            // 在这里添加字段过滤器
            //$filter->like('name', 'name');
            $filter->column(1/2, function ($filter) {
                $filter->like('title', '电影标题');
                $filter->between('rate', '评分');
            });
            
            $filter->column(1/2, function ($filter) {
                $filter->equal('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '更新时间')->datetime();
                $filter->equal('released', '是否上映')->radio([
                    1 => '是',
                    0 => '否',
                ]);
            });
        
        });
        

        

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Movie::findOrFail($id));

        $show->field('id', __('Id'));
        // $show->field('title', __('电影标题'));
        $show->field('title', __('Title'));
        $show->field('director', __('Director'));
        $show->field('describe', __('Describe'));
        $show->field('rate', __('Rate'));
        $show->field('created_at', __('Created_at'));
        $show->field('updated_at', __('Updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Movie);

        $form->text('title', __('Title'))->required();
        $directors = [
            1 => 'John',
            2 => 'Smith',
            3 => 'Kate' ,
        ];
        
        $form->select('director', __('Director'))->options($directors)->required();
        $form->textarea('describe', __('Describe'))->rows(5)->required();
        $form->number('rate', __('Rate'))->min(0)->required();

        // $states = [
        //     'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
        //     'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        // ];
        //$form->switch('released', '是否上映')->states($states); 


        // 添加开关操作
        //$form->switch('released', '是否上映')->default(1); 
        $form->radio('released', __('Released'))->options(['1' => '是', '0'=> '否'])->default('1');
       
        $form->datetime('release_at', __('Release_at'))->default(date('Y-m-d H:i:s'));
        $form->confirm('确定创建吗？', 'create');
        // 两个时间显示
        // $form->display('created_at', '创建时间');
        // $form->display('updated_at', '修改时间');
        
        // $form->submitted(function (Form $form) {
        //     $released = $form->released == "on" ? '1' : 0;
        //     $form->released = $released;
        // });
        // $form->saving(function (Form $form1) {
        //     $states = $form1->released == "on" ? 1 : 0;
        //     $form1->released = $states;
        //     //var_dump($form->released);die;
        // });

        return $form;
    }


}
